﻿using System;

namespace Jhong.FunMVC.Razor
{
    public class RazorViewResult : ViewResult
    {
        public RazorViewResult(Response response)
        {
            this._response = response;
        }

        public override void Execute()
        {
            var view = new View(this);
            var thtml = view.GetTempalteHtml();
            var viewEngine = EngineFactory.Engine;
            viewEngine.AddNamespace("Jhong.FunMVC");
            var result = viewEngine.RenderTemplate(thtml, model: new DynamicViewDataDictionary(() => this.ViewData, () => this.Model));
            if (false == string.IsNullOrWhiteSpace(viewEngine.ErrorMessage))
            {
                throw new Exception(viewEngine.ErrorMessage);
            }

            this._response.ContentType = @"text/html;charset=utf-8";
            this._response.Write(result);
        }
    }
}